package com.fz.us.oa.web.controller.com;

import com.fz.us.admin.base.bean.BaseEnumManager;
import com.fz.us.admin.base.bean.search.SearchEnum;
import com.fz.us.admin.base.bean.search.order.Order;
import com.fz.us.admin.base.bean.search.result.SearchResultPaging;
import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.bean.search.rule.RuleHelper;
import com.fz.us.admin.base.utils.DataUtil;
import com.fz.us.admin.base.utils.ZipUtil;
import com.fz.us.admin.company.entity.Duty;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.dict.entity.DictItem;
import com.fz.us.base.bean.BaseEnum;
import com.fz.us.oa.core.entity.com.*;
import com.fz.us.oa.core.entity.common.FileManage;
import com.fz.us.oa.core.service.com.*;
import com.fz.us.oa.core.service.common.FileManagerService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import com.mongodb.gridfs.GridFSDBFile;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URLDecoder;
import java.util.*;
import java.util.zip.ZipOutputStream;

/**
 * Created by dzw on 2018/3/28.
 * 街道去组织部导入数据和导出数据
 */
@Controller
@RequestMapping("/com/dataUpdate")
public class DataUpdateController extends BaseController {
    private static final long serialVersionUID = -976208157656141911L;

    @Autowired
    private PersonService personService;
    @Autowired
    private DirectorService directorService;
    @Autowired
    private StudyService studyService;
    @Autowired
    private WorkService workService;
    @Autowired
    private FamilyService familyService;
    @Autowired
    private FileManagerService fileManagerService;
    @Autowired
    private ResidentService residentService;

    /**
     * 跳转到导出页面
     */
    @RequestMapping(value = "toOutPage", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String toOutPage(@CurrentUser User user, Model model){
        List<DictItem> streetList = new ArrayList<DictItem>();
        if(StringUtils.isNotEmpty(user.getStreetIds())){
            for(String sid:user.getStreetIds().split(",")){
                streetList.add(dictItemService.get(sid.trim()));
            }
        }
        model.addAttribute("streetList", streetList);
        return "/dataUpdate/toOutPage";
    }

    /**
     * 跳转到导入页面
     */
    @RequestMapping(value = "toInPage", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String toInPage(@CurrentUser User user, Model model){
        return "/dataUpdate/toInPage";
    }

    /**
     * 导出数据（书记）
     */
    @RequestMapping(value = "export_sj", method = RequestMethod.POST)
    public Object export(@CurrentUser User user, String streetId, String filepath) throws IOException {

        DictItem street = dictItemService.get(streetId);
        if(street==null){
            return ajaxJson(resultService.fail("没有找到此街道"));
        }

//        String parPath = "C:\\data_sj"; //指定一个目录用来存放数据
        String parPath = "\\temp\\data_sj"; //指定一个目录用来存放数据
        File dir = new File(parPath+"\\photo");
        if(!dir.exists()){
            dir.mkdirs();
        }

        Order order = Order.order("createTime", SearchEnum.OrderType.asc);
        List<Rule> rules = new ArrayList<Rule>();
        rules.add(RuleHelper.eq("street", street));
        SearchResultPaging searchResult = personService.findPagingBySearch(null, rules, order);
        List<Person> personList = searchResult.getList();

        File file=new File(URLDecoder.decode(filepath, "UTF-8"));
        OutputStream outputStream = null;
        InputStream inputStream = new FileInputStream(file);
        Workbook workbook = new HSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0); //获取第一张sheet


        if(personList.size()==0){
            return ajaxJson(resultService.fail("根据选择的条件没有找到任何人员"));
        }

        for(int i=0;i<personList.size();i++){
            Person person = personList.get(i);
            Row row = sheet.createRow(i); //创建一行
            row.createCell(0).setCellValue(StringUtils.isNotEmpty(person.getKeyId())?person.getKeyId():person.getId()); //id
            row.createCell(1).setCellValue(person.getState().name()); //状态
            row.createCell(2).setCellValue(StringUtils.isNotEmpty(person.getName())?person.getName():""); //姓名
            row.createCell(3).setCellValue(StringUtils.isNotEmpty(person.getGender())?person.getGender():""); //性别
            row.createCell(4).setCellValue(person.getBirthday()!=null?DataUtil.DateToString(person.getBirthday(), "yyyy-MM-dd"):""); //生日
            row.createCell(5).setCellValue(person.getNation()!=null?person.getNation().getId():""); //民族
            row.createCell(6).setCellValue(StringUtils.isNotEmpty(person.getOrigin())?person.getOrigin():""); //籍贯
            row.createCell(7).setCellValue(StringUtils.isNotEmpty(person.getBirthPlace())?person.getBirthPlace():""); //出生地
            row.createCell(8).setCellValue(person.getJoinPartyDate()!=null?DataUtil.DateToString(person.getJoinPartyDate()):""); //入党时间
            row.createCell(9).setCellValue(person.getJoinWorkDate()!=null?DataUtil.DateToString(person.getJoinWorkDate()):""); //参加工作时间
            row.createCell(10).setCellValue(StringUtils.isNotEmpty(person.getHealth())?person.getHealth():""); //健康状况
            row.createCell(11).setCellValue(StringUtils.isNotEmpty(person.getTxtTecDuty())?person.getTxtTecDuty():""); //专业技术职务
            row.createCell(12).setCellValue(StringUtils.isNotEmpty(person.getSpecialty())?person.getSpecialty():""); //熟悉专业有何专长
            row.createCell(13).setCellValue(person.getWorkNature()!=null?person.getWorkNature().getId():""); //工作性质
            row.createCell(14).setCellValue(person.getPersonNature()!=null?person.getPersonNature().getId():""); //人员性质
            row.createCell(15).setCellValue(person.getHighEdu()!=null?person.getHighEdu().getId():""); //最高学历
            row.createCell(16).setCellValue(person.getAllDayEdu()!=null?person.getAllDayEdu().getId():""); //全日制教育
            row.createCell(17).setCellValue(StringUtils.isNotEmpty(person.getAllDaySchool())?person.getAllDaySchool():""); //毕业院校系及专业
            row.createCell(18).setCellValue(person.getJobEdu()!=null?person.getJobEdu().getId(): ""); //在职教育
            row.createCell(19).setCellValue(StringUtils.isNotEmpty(person.getJobSchool())?person.getJobSchool():""); //毕业院校系及专业
            row.createCell(20).setCellValue(StringUtils.isNotEmpty(person.getNowPost())?person.getNowPost():""); //现任职务
            row.createCell(21).setCellValue(person.getNowPostDate()!=null?DataUtil.DateToString(person.getNowPostDate(), "yyyy-MM-dd"):""); //现任职时间
            row.createCell(22).setCellValue(StringUtils.isNotEmpty(person.getArea())?person.getArea():""); //所在区
            row.createCell(23).setCellValue(person.getStreet()!=null?person.getStreet().getId():""); //所在街道
            row.createCell(24).setCellValue(person.getResident()!=null?person.getResident().getId():""); //居民区
            row.createCell(25).setCellValue(person.getPhoto()!=null?person.getPhoto().getId():""); //照片id
            row.createCell(26).setCellValue(StringUtils.isNotEmpty(person.getReward())?person.getReward():""); //奖惩情况
            row.createCell(27).setCellValue(String.valueOf(person.getShoulder())); //一肩挑书记情况 0否 1一肩挑
            row.createCell(28).setCellValue(StringUtils.isNotEmpty(person.getShoulderDet())?person.getShoulderDet():""); //一肩挑书记详情 已一肩挑，拟一肩挑连任，拟一肩挑备选
            row.createCell(29).setCellValue(StringUtils.isNotEmpty(person.getFamilyAddress())?person.getFamilyAddress():""); //家庭地址
            row.createCell(30).setCellValue(StringUtils.isNotEmpty(person.getPhone())?person.getPhone():""); //联系电话
            row.createCell(31).setCellValue(StringUtils.isNotEmpty(person.getZipCode())?person.getZipCode():""); //邮编

            //导出人员照片
            FileManage fileManage = person.getPhoto();
            if(fileManage!=null){
                GridFSDBFile gridFile = fileManagerService.gridFSGet(fileManage.getGridId());
                if(gridFile!=null){
                    OutputStream out = null;
                    if (gridFile.getContentType() != null) {
                        response.setContentType(gridFile.getContentType());
                    }
                    String suffix = gridFile.getFilename().substring(gridFile.getFilename().lastIndexOf("."), gridFile.getFilename().length());
                    try {
                        out = new FileOutputStream(parPath+"\\photo\\"+fileManage.getId() + suffix +""); //用原来的fileId作为文件名
                        gridFile.writeTo(out);
                        out.close();
                    }catch (FileNotFoundException e){
                        e.printStackTrace();
                    }

                }

            }

            //学习经历
            List<Study> studyList = person.getStudyList();
            Map<String, Object> sMap;
            List<Map<String, Object>> sList = new ArrayList<Map<String, Object>>();
            for(Study study:studyList){
                sMap = new HashMap<String, Object>();
                sMap.put("state", study.getState().name());
                sMap.put("startDate", study.getStartDate()!=null?DataUtil.DateToString(study.getStartDate(), "yyyy-MM-dd"):"");
                sMap.put("endDate", study.getEndDate()!=null?DataUtil.DateToString(study.getEndDate(),"yyyy-MM-dd"):"");
                sMap.put("school", StringUtils.isNotEmpty(study.getSchool())?study.getSchool():"");
                sMap.put("academicDegree", StringUtils.isNotEmpty(study.getAcademicDegree())?study.getAcademicDegree():"");
                sList.add(sMap);
            }
            String studyJson = JSONArray.fromObject(sList).toString();
            row.createCell(32).setCellValue(studyJson); //学习经历

            //工作经历
            List<Work> workList = person.getWorkList();
            Map<String, Object> wMap;
            List<Map<String, Object>> wList = new ArrayList<Map<String, Object>>();
            for(Work work:workList){
                wMap = new HashMap<String, Object>();
                wMap.put("state", work.getState().name());
                wMap.put("workStartDate", work.getWorkStartDate()!=null?DataUtil.DateToString(work.getWorkStartDate(), "yyyy-MM-dd"):"");
                wMap.put("workEndDate", work.getWorkEndDate()!=null?DataUtil.DateToString(work.getWorkEndDate(),"yyyy-MM-dd"):"");
                wMap.put("workDept", StringUtils.isNotEmpty(work.getWorkDept())?work.getWorkDept():"");
                wMap.put("workPost", StringUtils.isNotEmpty(work.getWorkPost())?work.getWorkPost():"");
                wMap.put("proveUser", StringUtils.isNotEmpty(work.getProveUser())?work.getProveUser():"");
                wList.add(wMap);
            }
            String workJson = JSONArray.fromObject(wList).toString();
            row.createCell(33).setCellValue(workJson); //工作经历

            //家庭信息
            List<Family> familyList = person.getFamilyList();
            Map<String, Object> fMap;
            List<Map<String, Object>> fList = new ArrayList<Map<String, Object>>();
            for(Family family:familyList){
                fMap = new HashMap<String, Object>();
                fMap.put("state", family.getState().name());
                fMap.put("nickName", StringUtils.isNotEmpty(family.getNickName())?family.getNickName():"");
                fMap.put("familyName", StringUtils.isNotEmpty(family.getFamilyName())?family.getFamilyName():"");
                fMap.put("familyBirthday", family.getFamilyBirthday()!=null?DataUtil.DateToString(family.getFamilyBirthday(),"yyyy-MM-dd"):"");
                fMap.put("politicalStatus", StringUtils.isNotEmpty(family.getPoliticalStatus())?family.getPoliticalStatus():"");
                fMap.put("familyEdu", StringUtils.isNotEmpty(family.getFamilyEdu())?family.getFamilyEdu():"");
                fMap.put("familyJob", StringUtils.isNotEmpty(family.getFamilyJob())?family.getFamilyJob():"");
                fList.add(fMap);
            }
            String familyJson = JSONArray.fromObject(fList).toString();
            row.createCell(34).setCellValue(familyJson); //家庭信息

            row.createCell(35).setCellValue(person.getGeneralDict()!=null?person.getGeneralDict().getId():"");
            row.createCell(36).setCellValue(StringUtils.isNotEmpty(person.getCardId())?person.getCardId():"");

        }
        try {
            outputStream = new FileOutputStream(parPath+"\\data_sj.xls");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            workbook.write(outputStream);
            if(outputStream!=null){
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        File reFile = new File(parPath);
        ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(reFile.getAbsolutePath() + ".zip")));
        String base = reFile.getName();
        ZipUtil.compressZip(zipOutput, reFile, base);
        zipOutput.closeEntry();
        zipOutput.close();
        ZipUtil.delFolder(parPath); //删除临时产生的文件夹

//        return ajaxJson(resultService.success("导出成功，请查看路径："+parPath));
        String downFileName = "data_sj_"+street.getPinYinHead()+".zip";
        File file2=new File(URLDecoder.decode(parPath+".zip", "UTF-8"));
        HttpHeaders headers = new HttpHeaders();
        String fileName = new String(downFileName.getBytes("UTF-8"), "ISO8859-1");
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file2), headers, HttpStatus.CREATED);
    }

    /**
     * 数据导出 主任
     */
    @RequestMapping(value = "export_zr", method = RequestMethod.POST)
    public Object export_zr(@CurrentUser User user, String streetId, String filepath) throws IOException {
        DictItem street = dictItemService.get(streetId);
        if(street==null){
            return ajaxJson(resultService.fail("没有找到此街道"));
        }

//        String parPath = "C:\\data_zr"; //指定一个目录用来存放数据
        String parPath = "\\temp\\data_zr"; //指定一个目录用来存放数据
        File dir = new File(parPath+"\\photo");
        if(!dir.exists()){
            dir.mkdirs();
        }

        Order order = Order.order("createTime", SearchEnum.OrderType.asc);
        List<Rule> rules = new ArrayList<Rule>();
        rules.add(RuleHelper.eq("street", street));
        SearchResultPaging searchResult = directorService.findPagingBySearch(null, rules, order);
        List<Director> personList = searchResult.getList();

        File file=new File(URLDecoder.decode(filepath, "UTF-8"));
        OutputStream outputStream = null;
        InputStream inputStream = new FileInputStream(file);
        Workbook workbook = new HSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0); //获取第一张sheet


        if(personList.size()==0){
            return ajaxJson(resultService.fail("根据选择的条件没有找到任何人员"));
        }

        for(int i=0;i<personList.size();i++){
            Director person = personList.get(i);
            Row row = sheet.createRow(i); //创建一行
            row.createCell(0).setCellValue(StringUtils.isNotEmpty(person.getKeyId())?person.getKeyId():person.getId()); //id
            row.createCell(1).setCellValue(person.getState().name()); //状态
            row.createCell(2).setCellValue(StringUtils.isNotEmpty(person.getName())?person.getName():""); //姓名
            row.createCell(3).setCellValue(StringUtils.isNotEmpty(person.getGender())?person.getGender():""); //性别
            row.createCell(4).setCellValue(person.getBirthday()!=null?DataUtil.DateToString(person.getBirthday(), "yyyy-MM-dd"):""); //生日
            row.createCell(5).setCellValue(person.getNation()!=null?person.getNation().getId():""); //民族
            row.createCell(6).setCellValue(StringUtils.isNotEmpty(person.getOrigin())?person.getOrigin():""); //籍贯
            row.createCell(7).setCellValue(StringUtils.isNotEmpty(person.getBirthPlace())?person.getBirthPlace():""); //出生地
            row.createCell(8).setCellValue(person.getJoinPartyDate()!=null?DataUtil.DateToString(person.getJoinPartyDate()):""); //入党时间
            row.createCell(9).setCellValue(person.getJoinWorkDate()!=null?DataUtil.DateToString(person.getJoinWorkDate()):""); //参加工作时间
            row.createCell(10).setCellValue(StringUtils.isNotEmpty(person.getHealth())?person.getHealth():""); //健康状况
            row.createCell(11).setCellValue(StringUtils.isNotEmpty(person.getTxtTecDuty())?person.getTxtTecDuty():""); //专业技术职务
            row.createCell(12).setCellValue(StringUtils.isNotEmpty(person.getSpecialty())?person.getSpecialty():""); //熟悉专业有何专长
            row.createCell(13).setCellValue(person.getWorkNature()!=null?person.getWorkNature().getId():""); //工作性质
            row.createCell(14).setCellValue(person.getPersonNature()!=null?person.getPersonNature().getId():""); //人员性质
            row.createCell(15).setCellValue(person.getHighEdu()!=null?person.getHighEdu().getId():""); //最高学历
            row.createCell(16).setCellValue(person.getAllDayEdu()!=null?person.getAllDayEdu().getId():""); //全日制教育
            row.createCell(17).setCellValue(StringUtils.isNotEmpty(person.getAllDaySchool())?person.getAllDaySchool():""); //毕业院校系及专业
            row.createCell(18).setCellValue(person.getJobEdu()!=null?person.getJobEdu().getId(): ""); //在职教育
            row.createCell(19).setCellValue(StringUtils.isNotEmpty(person.getJobSchool())?person.getJobSchool():""); //毕业院校系及专业
            row.createCell(20).setCellValue(StringUtils.isNotEmpty(person.getNowPost())?person.getNowPost():""); //现任职务
            row.createCell(21).setCellValue(person.getNowPostDate()!=null?DataUtil.DateToString(person.getNowPostDate(), "yyyy-MM-dd"):""); //现任职时间
            row.createCell(22).setCellValue(StringUtils.isNotEmpty(person.getArea())?person.getArea():""); //所在区
            row.createCell(23).setCellValue(person.getStreet()!=null?person.getStreet().getId():""); //所在街道
            row.createCell(24).setCellValue(person.getResident()!=null?person.getResident().getId():""); //居民区
            row.createCell(25).setCellValue(person.getPhoto()!=null?person.getPhoto().getId():""); //照片id
            row.createCell(26).setCellValue(StringUtils.isNotEmpty(person.getReward())?person.getReward():""); //奖惩情况
            row.createCell(27).setCellValue(String.valueOf(person.getShoulder())); //一肩挑书记情况 0否 1一肩挑
            row.createCell(28).setCellValue(StringUtils.isNotEmpty(person.getShoulderDet())?person.getShoulderDet():""); //一肩挑书记详情 已一肩挑，拟一肩挑连任，拟一肩挑备选
            row.createCell(29).setCellValue(StringUtils.isNotEmpty(person.getFamilyAddress())?person.getFamilyAddress():""); //家庭地址
            row.createCell(30).setCellValue(StringUtils.isNotEmpty(person.getPhone())?person.getPhone():""); //联系电话
            row.createCell(31).setCellValue(StringUtils.isNotEmpty(person.getZipCode())?person.getZipCode():""); //邮编

            //导出人员照片
            FileManage fileManage = person.getPhoto();
            if(fileManage!=null){
                GridFSDBFile gridFile = fileManagerService.gridFSGet(fileManage.getGridId());
                if(gridFile!=null){
                    OutputStream out = null;
                    if (gridFile.getContentType() != null) {
                        response.setContentType(gridFile.getContentType());
                    }
                    String suffix = gridFile.getFilename().substring(gridFile.getFilename().lastIndexOf("."), gridFile.getFilename().length());
                    try {
                        out = new FileOutputStream(parPath+"\\photo\\"+fileManage.getId() + suffix +""); //用原来的fileId作为文件名
                        gridFile.writeTo(out);
                        out.close();
                    }catch (FileNotFoundException e){
                        e.printStackTrace();
                    }

                }

            }

            //学习经历
            List<Study> studyList = person.getStudyList();
            Map<String, Object> sMap;
            List<Map<String, Object>> sList = new ArrayList<Map<String, Object>>();
            for(Study study:studyList){
                sMap = new HashMap<String, Object>();
                sMap.put("state", study.getState().name());
                sMap.put("startDate", study.getStartDate()!=null?DataUtil.DateToString(study.getStartDate(), "yyyy-MM-dd"):"");
                sMap.put("endDate", study.getEndDate()!=null?DataUtil.DateToString(study.getEndDate(),"yyyy-MM-dd"):"");
                sMap.put("school", StringUtils.isNotEmpty(study.getSchool())?study.getSchool():"");
                sMap.put("academicDegree", StringUtils.isNotEmpty(study.getAcademicDegree())?study.getAcademicDegree():"");
                sList.add(sMap);
            }
            String studyJson = JSONArray.fromObject(sList).toString();
            row.createCell(32).setCellValue(studyJson); //学习经历

            //工作经历
            List<Work> workList = person.getWorkList();
            Map<String, Object> wMap;
            List<Map<String, Object>> wList = new ArrayList<Map<String, Object>>();
            for(Work work:workList){
                wMap = new HashMap<String, Object>();
                wMap.put("state", work.getState().name());
                wMap.put("workStartDate", work.getWorkStartDate()!=null?DataUtil.DateToString(work.getWorkStartDate(), "yyyy-MM-dd"):"");
                wMap.put("workEndDate", work.getWorkEndDate()!=null?DataUtil.DateToString(work.getWorkEndDate(),"yyyy-MM-dd"):"");
                wMap.put("workDept", StringUtils.isNotEmpty(work.getWorkDept())?work.getWorkDept():"");
                wMap.put("workPost", StringUtils.isNotEmpty(work.getWorkPost())?work.getWorkPost():"");
                wMap.put("proveUser", StringUtils.isNotEmpty(work.getProveUser())?work.getProveUser():"");
                wList.add(wMap);
            }
            String workJson = JSONArray.fromObject(wList).toString();
            row.createCell(33).setCellValue(workJson); //工作经历

            //家庭信息
            List<Family> familyList = person.getFamilyList();
            Map<String, Object> fMap;
            List<Map<String, Object>> fList = new ArrayList<Map<String, Object>>();
            for(Family family:familyList){
                fMap = new HashMap<String, Object>();
                fMap.put("state", family.getState().name());
                fMap.put("nickName", StringUtils.isNotEmpty(family.getNickName())?family.getNickName():"");
                fMap.put("familyName", StringUtils.isNotEmpty(family.getFamilyName())?family.getFamilyName():"");
                fMap.put("familyBirthday", family.getFamilyBirthday()!=null?DataUtil.DateToString(family.getFamilyBirthday(),"yyyy-MM-dd"):"");
                fMap.put("politicalStatus", StringUtils.isNotEmpty(family.getPoliticalStatus())?family.getPoliticalStatus():"");
                fMap.put("familyEdu", StringUtils.isNotEmpty(family.getFamilyEdu())?family.getFamilyEdu():"");
                fMap.put("familyJob", StringUtils.isNotEmpty(family.getFamilyJob())?family.getFamilyJob():"");
                fList.add(fMap);
            }
            String familyJson = JSONArray.fromObject(fList).toString();
            row.createCell(34).setCellValue(familyJson); //家庭信息

            row.createCell(35).setCellValue(StringUtils.isNotEmpty(person.getZzmm())?person.getZzmm():""); //政治面貌（主任比书记多的一个字段！！！）
            row.createCell(36).setCellValue(person.getGeneralDict()!=null?person.getGeneralDict().getId():"");
            row.createCell(37).setCellValue(StringUtils.isNotEmpty(person.getCardId())?person.getCardId():"");
        }
        try {
            outputStream = new FileOutputStream(parPath+"\\data_zr.xls");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            workbook.write(outputStream);
            if(outputStream!=null){
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        return ajaxJson(resultService.success("导出成功，请查看路径："+parPath));

        File reFile = new File(parPath);
        ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(reFile.getAbsolutePath() + ".zip")));
        String base = reFile.getName();
        ZipUtil.compressZip(zipOutput, reFile, base);
        zipOutput.closeEntry();
        zipOutput.close();
        ZipUtil.delFolder(parPath); //删除临时产生的文件夹

        String downFileName = "data_zr_"+street.getPinYinHead()+".zip";
        File file2=new File(URLDecoder.decode(parPath+".zip", "UTF-8"));
        HttpHeaders headers = new HttpHeaders();
        String fileName = new String(downFileName.getBytes("UTF-8"), "ISO8859-1");
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file2), headers, HttpStatus.CREATED);

    }


    /**
     * 导入数据
     */
    @RequestMapping(value = "importData", method = RequestMethod.POST)
    public Object importData(@CurrentUser User user, String userType, HttpServletRequest request) throws IOException {
        if(StringUtils.isEmpty(userType)){
            return ajaxJson(resultService.fail("未获取到人员类型"));
        }
        String parPath = "\\temp"; //指定一个目录用来存放数据

        //创建一个通用的多部分解析器
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        //判断 request 是否有文件上传,即多部分请求
        if (multipartResolver.isMultipart(request)) {
            //转换成多部分request
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            //取得request中的所有文件名
            Iterator<String> iter = multiRequest.getFileNames();
            while (iter.hasNext()) {
                //取得上传文件
                MultipartFile file = multiRequest.getFile(iter.next());
                if (file != null) {
                    //取得当前上传文件的文件名称
                    String myFileName = file.getOriginalFilename();
                    //如果名称不为“”,说明该文件存在，否则说明该文件不存在
                    if (!"".equals(myFileName.trim())) {
                        Map<String, Object> data = new HashMap<String, Object>();
                        FileManage fileManage = fileManagerService.gridFSSave(file.getInputStream(), "APPLICATION/OCTET-STREAM", myFileName);
                        GridFSDBFile dbFile = fileManagerService.gridFSGet(fileManage.getGridId());
                        InputStream inputStream = dbFile.getInputStream();
                        ZipUtil.decompression(inputStream, parPath);

                        if("person".equals(userType)){
                            return import_sj(user);
                        }else if("director".equals(userType)){
                            return import_zr(user);
                        }
                    }
                }
            }
        }

        return ajaxJson(resultService.success("解压成功"));

    }


    public Object import_sj(User user){

        InputStream inputStream = null;
        String parPath = "\\temp\\data_sj";
        List<File> fileList = new ArrayList<File>();

        try {
            //获取主要的excel数据
            File file = new File(parPath+"\\data_sj.xls");
            inputStream = new FileInputStream(file);

            //获取所有照片
            File photos = new File(parPath+"\\photo");
            String[] fileArr = photos.list();
            for(int f=0;f<fileArr.length;f++){
                File readfile = new File(parPath + "\\photo\\" + fileArr[f]);
                fileList.add(readfile);
            }

        }catch (FileNotFoundException e){
            return ajaxJson(resultService.fail("没有找到对应的数据"));
        }


        Workbook workbook = null;
        try {
            workbook = new HSSFWorkbook(inputStream);
        } catch (IOException e) {
            return ajaxJson(resultService.fail("没有获取到对应的文件流"));
        }
        workbook.setActiveSheet(0);
        Sheet sheet = workbook.getSheetAt(0);
        int rowNum = sheet.getLastRowNum();
        for(int i=0; i<=rowNum; i++){
            System.out.println("书记：正在进行第"+(i+1)+"项，共"+(rowNum+1)+"项");
            HSSFRow row = (HSSFRow) sheet.getRow(i);
            if(row == null){
                continue;
            }

            String keyId=row.getCell(0)==null?"":row.getCell(0).getStringCellValue();
            Person person = null;
            if(personService.get(keyId)!=null){
                person = personService.get(keyId);
            }else if(personService.getByKeyId(keyId)!=null){
                person = personService.getByKeyId(keyId);
            }else{
                person = new Person();
                person.setCreater(user);
                person.setCompany(user.getCompany());
            }

            person.setKeyId(keyId);

            String state = row.getCell(1)==null?"":row.getCell(1).getStringCellValue();
            person.setState("Enable".endsWith(state) ? BaseEnumManager.StateEnum.Enable : BaseEnumManager.StateEnum.Delete); //状态
            person.setName(row.getCell(2) == null ? "" : row.getCell(2).getStringCellValue()); //姓名
            person.setGender(row.getCell(3) == null ? "" : row.getCell(3).getStringCellValue()); //性别
            person.setBirthday(row.getCell(4) == null ? null : DataUtil.StringToDate(row.getCell(4).getStringCellValue(), "yyyy-MM-dd")); //出生日期
            person.setNation(row.getCell(5) == null ? null : dictItemService.get(row.getCell(5).getStringCellValue())); //民族
            person.setOrigin(row.getCell(6) == null ? "" : row.getCell(6).getStringCellValue()); //籍贯
            person.setBirthPlace(row.getCell(7) == null ? "" : row.getCell(7).getStringCellValue()); //出生地
            person.setJoinPartyDate(row.getCell(8) == null ? null : DataUtil.StringToDate(row.getCell(8).getStringCellValue(), "yyyy-MM-dd")); //入党时间
            person.setJoinWorkDate(row.getCell(9) == null ? null : DataUtil.StringToDate(row.getCell(9).getStringCellValue(), "yyyy-MM-dd")); //参加工作时间
            person.setHealth(row.getCell(10) == null ? "" : row.getCell(10).getStringCellValue()); //健康状况
            person.setTxtTecDuty(row.getCell(11) == null ? "" : row.getCell(11).getStringCellValue()); //专业技术职务（这个目前没有用，隐藏掉）
            person.setSpecialty(row.getCell(12) == null ? "" : row.getCell(12).getStringCellValue()); //熟悉专业有何专长
            person.setWorkNature(row.getCell(13) == null ? null : dictItemService.get(row.getCell(13).getStringCellValue())); //工作性质
            person.setPersonNature(row.getCell(14) == null ? null : dictItemService.get(row.getCell(14).getStringCellValue())); //人员性质
            person.setHighEdu(row.getCell(15) == null ? null : dictItemService.get(row.getCell(15).getStringCellValue())); //最高学历
            person.setAllDayEdu(row.getCell(16) == null ? null : dictItemService.get(row.getCell(16).getStringCellValue())); //全日制教育
            person.setAllDaySchool(row.getCell(17) == null ? "" : row.getCell(17).getStringCellValue()); //毕业院校系及专业
            person.setJobEdu(row.getCell(18) == null ? null : dictItemService.get(row.getCell(18).getStringCellValue())); //在职教育
            person.setJobSchool(row.getCell(19) == null ? "" : row.getCell(19).getStringCellValue()); //毕业院校系及专业
            person.setNowPost(row.getCell(20) == null ? "" : row.getCell(20).getStringCellValue()); //现任职务
            person.setNowPostDate(row.getCell(21) == null ? null : DataUtil.StringToDate(row.getCell(21).getStringCellValue(), "yyyy-MM-dd")); //现任职时间
            person.setArea(row.getCell(22) == null ? "" : row.getCell(22).getStringCellValue()); //所在区
            person.setStreet(row.getCell(23) == null ? null : dictItemService.get(row.getCell(23).getStringCellValue())); //所在街道
            person.setResident(row.getCell(24) == null ? null : residentService.get(row.getCell(24).getStringCellValue())); //居民区

            String photoId = row.getCell(25) == null ? "" : row.getCell(25).getStringCellValue();
            if(StringUtils.isNotEmpty(photoId)){
                FileManage fileManage = fileManagerService.get(photoId);
                if(fileManage!=null){
                    System.out.println("找到了自己的照片！photoId："+photoId);
                    person.setPhoto(fileManage);
                }else{
                    for(File file:fileList){
                        if(file.getName().contains(photoId)){
                            System.out.println("没有找到自己的照片...photoId："+photoId);
                            InputStream photoInputStream = null;
                            try {
                                photoInputStream = new FileInputStream(file);
                                FileManage photoFileManage = fileManagerService.gridFSSave(photoInputStream, "APPLICATION/OCTET-STREAM", file.getName());
                                person.setPhoto(photoFileManage);
                            } catch (FileNotFoundException e) {
                                return ajaxJson(resultService.fail("第"+(i+1)+"行人员的照片没有找到"));
                            } catch (IOException e) {
                                return ajaxJson(resultService.fail("在保存第"+(i+1)+"行人员时出错"));
                            }

                        }
                    }
                }
            }

            person.setReward(row.getCell(26) == null ? "" : row.getCell(26).getStringCellValue()); //奖惩情况
            person.setShoulder(row.getCell(27) == null ? 0 : Integer.parseInt(row.getCell(27).getStringCellValue())); //一肩挑书记情况
            person.setShoulderDet(row.getCell(28) == null ? "" : row.getCell(28).getStringCellValue()); //奖惩情况
            person.setFamilyAddress(row.getCell(29) == null ? "" : row.getCell(29).getStringCellValue()); //家庭地址
            person.setPhone(row.getCell(30) == null ? "" : row.getCell(30).getStringCellValue()); //联系电话
            person.setZipCode(row.getCell(31) == null ? "" : row.getCell(31).getStringCellValue()); //邮编

            //学习经历
            String studyJson = row.getCell(32) == null ? "" : row.getCell(32).getStringCellValue();
            if(StringUtils.isNotEmpty(studyJson)){
                List<Study> studyList = new ArrayList<Study>();
                JSONArray studyJsonArr = JSONArray.fromObject(studyJson);
                for(int j=0;j<studyJsonArr.size();j++){
                    JSONObject studyJsonObj = studyJsonArr.getJSONObject(j);
                    if(studyJsonObj!=null){
                        Study study = new Study();
                        String startDate = studyJsonObj.getString("startDate");
                        String endDate = studyJsonObj.getString("endDate");
                        String school = studyJsonObj.getString("school");
                        String academicDegree = studyJsonObj.getString("academicDegree");
                        if(StringUtils.isNotEmpty(startDate)){
                            study.setStartDate(DataUtil.StringToDate(startDate,"yyyy-MM-dd"));
                        }
                        if(StringUtils.isNotEmpty(endDate)){
                            study.setEndDate(DataUtil.StringToDate(endDate, "yyyy-MM-dd"));
                        }
                        if(StringUtils.isNotEmpty(school)){
                            study.setSchool(school);
                        }
                        if(StringUtils.isNotEmpty(academicDegree)){
                            study.setAcademicDegree(academicDegree);
                        }
                        studyService.save(study);
                        studyList.add(study);
                    }
                }
                person.setStudyList(studyList);
            }

            //工作经历
            String workJson = row.getCell(33) == null ? "" : row.getCell(33).getStringCellValue();
            if(StringUtils.isNotEmpty(workJson)){
                List<Work> workList = new ArrayList<Work>();
                JSONArray workJsonArr = JSONArray.fromObject(workJson);
                for(int j=0;j<workJsonArr.size();j++){
                    JSONObject workJsonObj = workJsonArr.getJSONObject(j);
                    if(workJsonObj!=null){
                        Work work = new Work();
                        String workStartDate = workJsonObj.getString("workStartDate");
                        String workEndDate = workJsonObj.getString("workEndDate");
                        String workDept = workJsonObj.getString("workDept");
                        String workPost = workJsonObj.getString("workPost");
                        String proveUser = workJsonObj.getString("proveUser");
                        if(StringUtils.isNotEmpty(workStartDate)){
                            work.setWorkStartDate(DataUtil.StringToDate(workStartDate, "yyyy-MM-dd"));
                        }
                        if(StringUtils.isNotEmpty(workEndDate)){
                            work.setWorkEndDate(DataUtil.StringToDate(workEndDate, "yyyy-MM-dd"));
                        }
                        if(StringUtils.isNotEmpty(workDept)){
                            work.setWorkDept(workDept);
                        }
                        if(StringUtils.isNotEmpty(workPost)){
                            work.setWorkPost(workPost);
                        }
                        if(StringUtils.isNotEmpty(proveUser)){
                            work.setProveUser(proveUser);
                        }
                        workService.save(work);
                        workList.add(work);
                    }
                }
                person.setWorkList(workList);
            }

            //家庭信息
            String familyJson = row.getCell(34) == null ? "" : row.getCell(34).getStringCellValue();
            if(StringUtils.isNotEmpty(familyJson)){
                List<Family> familyList = new ArrayList<Family>();
                JSONArray familyJsonArr = JSONArray.fromObject(familyJson);
                for(int j=0;j<familyJsonArr.size();j++){
                    JSONObject familyJsonObj = familyJsonArr.getJSONObject(j);
                    if(familyJsonObj!=null){
                        Family family = new Family();
                        String nickName = familyJsonObj.getString("nickName");
                        String familyName = familyJsonObj.getString("familyName");
                        String familyBirthday = familyJsonObj.getString("familyBirthday");
                        String politicalStatus = familyJsonObj.getString("politicalStatus");
                        String familyEdu = familyJsonObj.getString("familyEdu");
                        String familyJob = familyJsonObj.getString("familyJob");
                        if(StringUtils.isNotEmpty(nickName)){
                            family.setNickName(nickName);
                        }
                        if(StringUtils.isNotEmpty(familyName)){
                            family.setFamilyName(familyName);
                        }
                        if(StringUtils.isNotEmpty(familyBirthday)){
                            family.setFamilyBirthday(DataUtil.StringToDate(familyBirthday, "yyyy-MM-dd"));
                        }
                        if(StringUtils.isNotEmpty(politicalStatus)){
                            family.setPoliticalStatus(politicalStatus);
                        }
                        if(StringUtils.isNotEmpty(familyEdu)){
                            family.setFamilyEdu(familyEdu);
                        }
                        if(StringUtils.isNotEmpty(familyJob)){
                            family.setFamilyJob(familyJob);
                        }
                        familyService.save(family);
                        familyList.add(family);
                    }
                }
                person.setFamilyList(familyList);
            }
            person.setGeneralDict(row.getCell(35) == null ? null : dictItemService.get(row.getCell(35).getStringCellValue())); //换届区间
            person.setCardId(row.getCell(36)==null?"":row.getCell(36).getStringCellValue()); //身份证号

            if(personService.get(keyId)!=null){
                personService.update(person);
            }else if(personService.getByKeyId(keyId)!=null){
                personService.update(person);
            }else{
                Duty duty = null;
                if(user.getDutySet()!=null && user.getDutySet().size()>0){
                    for(Duty duty1 : user.getDutySet()){
                        duty = duty1;
                        break;
                    }
                }
                personService.commit(person, "com.fz.us.oa.core.entity.com.Person", duty);
            }

        }

        System.out.println("书记数据导入完成");
        return ajaxJson(resultService.success("导入成功"));
    }

    public Object import_zr(User user) {
        InputStream inputStream = null;
        String parPath = "\\temp\\data_zr";
        List<File> fileList = new ArrayList<File>();

        try {
            //获取主要的excel数据
            File file = new File(parPath+"\\data_zr.xls");
            inputStream = new FileInputStream(file);

            //获取所有照片
            File photos = new File(parPath+"\\photo");
            String[] fileArr = photos.list();
            for(int f=0;f<fileArr.length;f++){
                File readfile = new File(parPath + "\\photo\\" + fileArr[f]);
                fileList.add(readfile);
            }

        }catch (FileNotFoundException e){
            return ajaxJson(resultService.fail("没有找到对应的数据"));
        }


        Workbook workbook = null;
        try {
            workbook = new HSSFWorkbook(inputStream);
        } catch (IOException e) {
            return ajaxJson(resultService.fail("没有找到对应的文件流"));
        }
        workbook.setActiveSheet(0);
        Sheet sheet = workbook.getSheetAt(0);
        int rowNum = sheet.getLastRowNum();
        for(int i=0; i<=rowNum; i++){
            System.out.println("主任：正在进行第"+(i+1)+"项，共"+(rowNum+1)+"项");
            HSSFRow row = (HSSFRow) sheet.getRow(i);
            if(row == null){
                continue;
            }

            String keyId=row.getCell(0)==null?"":row.getCell(0).getStringCellValue();
            Director person = null;
            if(directorService.get(keyId)!=null){
                person = directorService.get(keyId);
            }else if(directorService.getByKeyId(keyId)!=null){
                person = directorService.getByKeyId(keyId);
            }else{
                person = new Director();
                person.setCreater(user);
                person.setCompany(user.getCompany());
            }

            person.setKeyId(keyId);

            String state = row.getCell(1)==null?"":row.getCell(1).getStringCellValue();
            person.setState("Enable".endsWith(state) ? BaseEnumManager.StateEnum.Enable : BaseEnumManager.StateEnum.Delete); //状态
            person.setName(row.getCell(2) == null ? "" : row.getCell(2).getStringCellValue()); //姓名
            person.setGender(row.getCell(3) == null ? "" : row.getCell(3).getStringCellValue()); //性别
            person.setBirthday(row.getCell(4) == null ? null : DataUtil.StringToDate(row.getCell(4).getStringCellValue(), "yyyy-MM-dd")); //出生日期
            person.setNation(row.getCell(5) == null ? null : dictItemService.get(row.getCell(5).getStringCellValue())); //民族
            person.setOrigin(row.getCell(6) == null ? "" : row.getCell(6).getStringCellValue()); //籍贯
            person.setBirthPlace(row.getCell(7) == null ? "" : row.getCell(7).getStringCellValue()); //出生地
            person.setJoinPartyDate(row.getCell(8) == null ? null : DataUtil.StringToDate(row.getCell(8).getStringCellValue(), "yyyy-MM-dd")); //入党时间
            person.setJoinWorkDate(row.getCell(9) == null ? null : DataUtil.StringToDate(row.getCell(9).getStringCellValue(), "yyyy-MM-dd")); //参加工作时间
            person.setHealth(row.getCell(10) == null ? "" : row.getCell(10).getStringCellValue()); //健康状况
            person.setTxtTecDuty(row.getCell(11) == null ? "" : row.getCell(11).getStringCellValue()); //专业技术职务（这个目前没有用，隐藏掉）
            person.setSpecialty(row.getCell(12) == null ? "" : row.getCell(12).getStringCellValue()); //熟悉专业有何专长
            person.setWorkNature(row.getCell(13) == null ? null : dictItemService.get(row.getCell(13).getStringCellValue())); //工作性质
            person.setPersonNature(row.getCell(14) == null ? null : dictItemService.get(row.getCell(14).getStringCellValue())); //人员性质
            person.setHighEdu(row.getCell(15) == null ? null : dictItemService.get(row.getCell(15).getStringCellValue())); //最高学历
            person.setAllDayEdu(row.getCell(16) == null ? null : dictItemService.get(row.getCell(16).getStringCellValue())); //全日制教育
            person.setAllDaySchool(row.getCell(17) == null ? "" : row.getCell(17).getStringCellValue()); //毕业院校系及专业
            person.setJobEdu(row.getCell(18) == null ? null : dictItemService.get(row.getCell(18).getStringCellValue())); //在职教育
            person.setJobSchool(row.getCell(19) == null ? "" : row.getCell(19).getStringCellValue()); //毕业院校系及专业
            person.setNowPost(row.getCell(20) == null ? "" : row.getCell(20).getStringCellValue()); //现任职务
            person.setNowPostDate(row.getCell(21) == null ? null : DataUtil.StringToDate(row.getCell(21).getStringCellValue(), "yyyy-MM-dd")); //现任职时间
            person.setArea(row.getCell(22) == null ? "" : row.getCell(22).getStringCellValue()); //所在区
            person.setStreet(row.getCell(23) == null ? null : dictItemService.get(row.getCell(23).getStringCellValue())); //所在街道
            person.setResident(row.getCell(24) == null ? null : residentService.get(row.getCell(24).getStringCellValue())); //居民区

            String photoId = row.getCell(25) == null ? "" : row.getCell(25).getStringCellValue();
            if(StringUtils.isNotEmpty(photoId)){
                FileManage fileManage = fileManagerService.get(photoId);
                if(fileManage!=null){
                    System.out.println("找到了自己的照片！photoId："+photoId);
                    person.setPhoto(fileManage);
                }else{
                    for(File file:fileList){
                        if(file.getName().contains(photoId)){
                            System.out.println("没有找到自己的照片...photoId："+photoId);
                            InputStream photoInputStream = null;
                            try {
                                photoInputStream = new FileInputStream(file);
                                FileManage photoFileManage = fileManagerService.gridFSSave(photoInputStream, "APPLICATION/OCTET-STREAM", file.getName());
                                person.setPhoto(photoFileManage);
                            } catch (FileNotFoundException e) {
                                return ajaxJson(resultService.fail("第"+(i+1)+"行人员的照片没有找到"));
                            } catch (IOException e) {
                                return ajaxJson(resultService.fail("在保存第"+(i+1)+"行人员时出错"));
                            }

                        }
                    }
                }
            }

            person.setReward(row.getCell(26) == null ? "" : row.getCell(26).getStringCellValue()); //奖惩情况
            person.setShoulder(row.getCell(27) == null ? 0 : Integer.parseInt(row.getCell(27).getStringCellValue())); //一肩挑书记情况
            person.setShoulderDet(row.getCell(28) == null ? "" : row.getCell(28).getStringCellValue()); //奖惩情况
            person.setFamilyAddress(row.getCell(29) == null ? "" : row.getCell(29).getStringCellValue()); //家庭地址
            person.setPhone(row.getCell(30) == null ? "" : row.getCell(30).getStringCellValue()); //联系电话
            person.setZipCode(row.getCell(31) == null ? "" : row.getCell(31).getStringCellValue()); //邮编

            //学习经历
            String studyJson = row.getCell(32) == null ? "" : row.getCell(32).getStringCellValue();
            if(StringUtils.isNotEmpty(studyJson)){
                List<Study> studyList = new ArrayList<Study>();
                JSONArray studyJsonArr = JSONArray.fromObject(studyJson);
                for(int j=0;j<studyJsonArr.size();j++){
                    JSONObject studyJsonObj = studyJsonArr.getJSONObject(j);
                    if(studyJsonObj!=null){
                        Study study = new Study();
                        String startDate = studyJsonObj.getString("startDate");
                        String endDate = studyJsonObj.getString("endDate");
                        String school = studyJsonObj.getString("school");
                        String academicDegree = studyJsonObj.getString("academicDegree");
                        if(StringUtils.isNotEmpty(startDate)){
                            study.setStartDate(DataUtil.StringToDate(startDate,"yyyy-MM-dd"));
                        }
                        if(StringUtils.isNotEmpty(endDate)){
                            study.setEndDate(DataUtil.StringToDate(endDate, "yyyy-MM-dd"));
                        }
                        if(StringUtils.isNotEmpty(school)){
                            study.setSchool(school);
                        }
                        if(StringUtils.isNotEmpty(academicDegree)){
                            study.setAcademicDegree(academicDegree);
                        }
                        studyService.save(study);
                        studyList.add(study);
                    }
                }
                person.setStudyList(studyList);
            }

            //工作经历
            String workJson = row.getCell(33) == null ? "" : row.getCell(33).getStringCellValue();
            if(StringUtils.isNotEmpty(workJson)){
                List<Work> workList = new ArrayList<Work>();
                JSONArray workJsonArr = JSONArray.fromObject(workJson);
                for(int j=0;j<workJsonArr.size();j++){
                    JSONObject workJsonObj = workJsonArr.getJSONObject(j);
                    if(workJsonObj!=null){
                        Work work = new Work();
                        String workStartDate = workJsonObj.getString("workStartDate");
                        String workEndDate = workJsonObj.getString("workEndDate");
                        String workDept = workJsonObj.getString("workDept");
                        String workPost = workJsonObj.getString("workPost");
                        String proveUser = workJsonObj.getString("proveUser");
                        if(StringUtils.isNotEmpty(workStartDate)){
                            work.setWorkStartDate(DataUtil.StringToDate(workStartDate, "yyyy-MM-dd"));
                        }
                        if(StringUtils.isNotEmpty(workEndDate)){
                            work.setWorkEndDate(DataUtil.StringToDate(workEndDate, "yyyy-MM-dd"));
                        }
                        if(StringUtils.isNotEmpty(workDept)){
                            work.setWorkDept(workDept);
                        }
                        if(StringUtils.isNotEmpty(workPost)){
                            work.setWorkPost(workPost);
                        }
                        if(StringUtils.isNotEmpty(proveUser)){
                            work.setProveUser(proveUser);
                        }
                        workService.save(work);
                        workList.add(work);
                    }
                }
                person.setWorkList(workList);
            }

            //家庭信息
            String familyJson = row.getCell(34) == null ? "" : row.getCell(34).getStringCellValue();
            if(StringUtils.isNotEmpty(familyJson)){
                List<Family> familyList = new ArrayList<Family>();
                JSONArray familyJsonArr = JSONArray.fromObject(familyJson);
                for(int j=0;j<familyJsonArr.size();j++){
                    JSONObject familyJsonObj = familyJsonArr.getJSONObject(j);
                    if(familyJsonObj!=null){
                        Family family = new Family();
                        String nickName = familyJsonObj.getString("nickName");
                        String familyName = familyJsonObj.getString("familyName");
                        String familyBirthday = familyJsonObj.getString("familyBirthday");
                        String politicalStatus = familyJsonObj.getString("politicalStatus");
                        String familyEdu = familyJsonObj.getString("familyEdu");
                        String familyJob = familyJsonObj.getString("familyJob");
                        if(StringUtils.isNotEmpty(nickName)){
                            family.setNickName(nickName);
                        }
                        if(StringUtils.isNotEmpty(familyName)){
                            family.setFamilyName(familyName);
                        }
                        if(StringUtils.isNotEmpty(familyBirthday)){
                            family.setFamilyBirthday(DataUtil.StringToDate(familyBirthday, "yyyy-MM-dd"));
                        }
                        if(StringUtils.isNotEmpty(politicalStatus)){
                            family.setPoliticalStatus(politicalStatus);
                        }
                        if(StringUtils.isNotEmpty(familyEdu)){
                            family.setFamilyEdu(familyEdu);
                        }
                        if(StringUtils.isNotEmpty(familyJob)){
                            family.setFamilyJob(familyJob);
                        }
                        familyService.save(family);
                        familyList.add(family);
                    }
                }
                person.setFamilyList(familyList);
            }

            person.setZzmm(row.getCell(35) == null ? "" : row.getCell(35).getStringCellValue()); //政治面貌
            person.setGeneralDict(row.getCell(36) == null ? null : dictItemService.get(row.getCell(36).getStringCellValue())); //换届区间
            person.setCardId(row.getCell(37)==null?"":row.getCell(37).getStringCellValue());

            if(directorService.get(keyId)!=null){
                directorService.update(person);
            }else if(directorService.getByKeyId(keyId)!=null){
                directorService.update(person);
            }else{
                Duty duty = null;
                if(user.getDutySet()!=null && user.getDutySet().size()>0){
                    for(Duty duty1 : user.getDutySet()){
                        duty = duty1;
                        break;
                    }
                }
                directorService.commit(person, "com.fz.us.oa.core.entity.com.Director", duty);
            }

        }


        System.out.println("主任数据导入完成");
        return ajaxJson(resultService.success("导入成功"));
    }


}
